from functools import wraps
from flask import session, request
from common.exceptions import AuthorizationFailed, IdentificationFailed, CsrfException


def login_validate(func):
    @wraps(func)
    def wrap(*args, **kwargs):
        # 未登录，认证失败
        if not session.get('login_status'):
            raise IdentificationFailed()
        if not session.get('x-csrf-token') == request.headers.get('x-csrf-token'):
            raise CsrfException()
        return func(*args, **kwargs)
    return wrap
